package com.joy.controller;

import object.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class TestController {

    @RequestMapping(value = "baseType.do")
    @ResponseBody
    public String baseType(int age) {
        return "age:"+age;
    }

    @RequestMapping(value = "baseType2.do")
    @ResponseBody
    public String baseType2(Integer age) {
        return "age:"+age;
    }


    @RequestMapping(value = "array.do")
    @ResponseBody
    public String array(String[] name) {
        StringBuilder sb = new StringBuilder();
        for (String item : name) {
            sb.append(item).append(" ");
        }
        return sb.toString();
    }

    @RequestMapping(value="object.do")
    @ResponseBody
    public String object(User user, Admin admin) {
        return user.toString()+" "+admin.toString();
    }


    @InitBinder("user")
    public void initUser(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("user.");
    }

    @InitBinder("admin")
    public void initAdmin(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("admin.");
    }


    @RequestMapping(value = "list.do")
    @ResponseBody
    public String list(UserListForm userList) {
        return userList.toString();
    }


    @RequestMapping(value = "set.do")
    @ResponseBody
    public String set(UserSetForm userSetForm) {
        return userSetForm.toString();
    }


    @RequestMapping(value = "map.do")
    @ResponseBody
    public String map(UsermapForm usermapForm) {
        return usermapForm.toString();
    }

    @RequestMapping(value = "json.do")
    @ResponseBody
    public String json(@RequestBody User user) {
        return user.toString();
    }


    @RequestMapping(value = "xml.do")
    @ResponseBody
    public String json(@RequestBody Admin admin) {
        return admin.toString();
    }

}
